package com.visikard.ui.activity;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.visikard.application.Constant;
import com.visikard.ui.R;
import com.visikard.ui.fragment.BaseFragment;
import com.visikard.ui.fragment.TransactionCouponKardFragment;
import com.visikard.ui.fragment.TransactionGiftKardFragment;
import com.visikard.ui.fragment.TransactionPunchKardFragment;
import com.visikard.ui.fragment.ViewClickable;
import com.visikard.widget.NavigationStateRelativeLayout;

public class TransactionHistoryActivity extends BaseLoginCompleteActivity implements ViewClickable {	
	
	public static final String KEY_STARTUP_FRAGMENT = "key_startup_fragment";
	private static final String TAG_GIFTKARD = "giftkard_fragment";
	private static final String TAG_COUPONKARD = "coupon_fragment";
	private static final String TAG_PUNCHKARD = "punchkard_fragment";
	private static final int CONTENT_ID_GIFTKARD = R.id.transaction_content_giftkard;
	private static final int CONTENT_ID_COUPON = R.id.transaction_content_coupon;
	private static final int CONTENT_ID_PUNCHKARD = R.id.transaction_content_punchkard;
	
	public enum ContentFragment{
		GIFTKARD, COUPON, PUNCHKARD;
	}

	private FragmentManager fmManager;
	private BaseFragment mGiftKardFragment, mCouponFragment, mPunchKardFragment;
	
	private NavigationStateRelativeLayout btnGiftKard;
	private NavigationStateRelativeLayout btnCouponKard;
	private NavigationStateRelativeLayout btnPunchKard;
	
	private View giftKardContainer, couponContainer, punchKardContainer;
	private ContentFragment mCurrentContentFragment;
	
	DateFormat dateOnlyFormatter, dateServerFormatter;
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		setContentView(R.layout.transaction_history_activity);
		
		giftKardContainer = findViewById(CONTENT_ID_GIFTKARD);
		couponContainer = findViewById(CONTENT_ID_COUPON);
		punchKardContainer = findViewById(CONTENT_ID_PUNCHKARD);
		
		fmManager = getSupportFragmentManager();
		
		btnGiftKard = (NavigationStateRelativeLayout) findViewById(R.id.tab_gift_kard);
		btnCouponKard = (NavigationStateRelativeLayout) findViewById(R.id.tab_coupon);
		btnPunchKard = (NavigationStateRelativeLayout) findViewById(R.id.tab_punch_kard);
		
		dateOnlyFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_MONTH_DATE, Locale.getDefault());
		dateServerFormatter = new SimpleDateFormat(Constant.TIME_FORMAT_STR_US_DATE, Locale.getDefault());
		
		mGiftKardFragment = new TransactionGiftKardFragment();
		mCouponFragment = new TransactionCouponKardFragment();
		mPunchKardFragment = new TransactionPunchKardFragment();

		//get the start up fragment
		mCurrentContentFragment = ContentFragment.GIFTKARD;
		if(getIntent().getSerializableExtra(KEY_STARTUP_FRAGMENT) != null){
			mCurrentContentFragment = (ContentFragment)getIntent().getSerializableExtra(KEY_STARTUP_FRAGMENT);
		}
		
		updateContentFragment();
		
	}
	
	private void updateContentFragment(){
		switch(mCurrentContentFragment){
		case GIFTKARD:
			btnGiftKard.setChecked(true);
			btnCouponKard.setChecked(false);
			btnPunchKard.setChecked(false);
			if(fmManager.findFragmentByTag(TAG_GIFTKARD) == null)
				fmManager.beginTransaction().add(CONTENT_ID_GIFTKARD, mGiftKardFragment, TAG_GIFTKARD).commit();
			giftKardContainer.setVisibility(View.VISIBLE);
			couponContainer.setVisibility(View.GONE);
			punchKardContainer.setVisibility(View.GONE);			
			break;
		case COUPON:
			btnGiftKard.setChecked(false);
			btnCouponKard.setChecked(true);
			btnPunchKard.setChecked(false);
			if(fmManager.findFragmentByTag(TAG_COUPONKARD) == null)
				fmManager.beginTransaction().add(CONTENT_ID_COUPON, mCouponFragment, TAG_COUPONKARD).commit();
			couponContainer.setVisibility(View.VISIBLE);
			punchKardContainer.setVisibility(View.GONE);
			giftKardContainer.setVisibility(View.GONE);			
			break;
		case PUNCHKARD:
			btnGiftKard.setChecked(false);
			btnCouponKard.setChecked(false);
			btnPunchKard.setChecked(true);
			if(fmManager.findFragmentByTag(TAG_PUNCHKARD) == null)
				fmManager.beginTransaction().add(CONTENT_ID_PUNCHKARD, mPunchKardFragment, TAG_PUNCHKARD).commit();
			punchKardContainer.setVisibility(View.VISIBLE);
			giftKardContainer.setVisibility(View.GONE);
			couponContainer.setVisibility(View.GONE);			
			break;
		}
	}
	
	@Override
	public void onViewClick(View v) {
		switch (v.getId()) {
		case R.id.home_btn_menu:
			finish();
			break;
		case R.id.tab_gift_kard:
			if(mCurrentContentFragment != ContentFragment.GIFTKARD){
				mCurrentContentFragment = ContentFragment.GIFTKARD;
				updateContentFragment();
			}
			btnGiftKard.setChecked(true);
			break;
		case R.id.tab_coupon:
			if(mCurrentContentFragment != ContentFragment.COUPON){
				mCurrentContentFragment = ContentFragment.COUPON;
				updateContentFragment();
			}
			btnCouponKard.setChecked(true);
			break;
		case R.id.tab_punch_kard:
			if(mCurrentContentFragment != ContentFragment.PUNCHKARD){
				mCurrentContentFragment = ContentFragment.PUNCHKARD;
				updateContentFragment();
			}
			btnPunchKard.setChecked(true);
			break;
		default:
			try {
				if (v instanceof ViewClickable) {
					((ViewClickable) v).onViewClick(v);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
	}
	
	public String getDateToday(){
		String strDate = "";
		Date today;
		try {
			today = dateOnlyFormatter.parse(dateOnlyFormatter.format(new Date()));
			Calendar cal = Calendar.getInstance();
			cal.setTime(today);
			strDate = dateOnlyFormatter.format(today);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return strDate;
	}

	public String getDateTodayServer(){
		String strDate = "";
		Date todayServer;
		try {
			todayServer = dateServerFormatter.parse(dateServerFormatter.format(new Date()));
			Calendar calServer = Calendar.getInstance();
			calServer.setTime(todayServer);
			strDate = dateServerFormatter.format(todayServer);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return strDate;
	}
	
	public String getMonth(int month) {
		String result = "";
		switch (month) {
		case 1:
			result = "Jan";
			break;
		case 2:
			result = "Feb";
			break;
		case 3:
			result = "Mar";
			break;
		case 4:
			result = "Apr";
			break;
		case 5:
			result = "May";
			break;
		case 6:
			result = "Jun";
			break;
		case 7:
			result = "Jul";
			break;
		case 8:
			result = "Aug";
			break;
		case 9:
			result = "Sep";
			break;
		case 10:
			result = "Oct";
			break;
		case 11:
			result = "Nov";
			break;
		case 12:
			result = "Dec";
			break;
		default:
			result = "Jan";
			break;
		}
		return result;
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
}
